#pragma once
#include<signal.h>
#include<pthread.h>
#include<cstdlib>
#include<unistd.h>
#include"TcpServer.hpp"
#include"Protocol.hpp"
#include"Util.hpp"

class HttpServer{
  public:
    HttpServer(int port = PORT):_port(port),stap(false)
    {
    }
    void initserver()
    {
      Log::LOG(INFO,"HttpServer is init");
      //signal信号设置
      signal(SIGPIPE,SIG_IGN);
    }
    void Loop()
    {
      TcpServer* ts = TcpServer::getinstance(_port);
      Log::LOG(INFO,"sever is loop");
      while(!stap)
      {
          struct sockaddr_in peer;
          socklen_t len = sizeof(peer);
          //获取连接
          int sock = accept(ts->Sock(),(struct sockaddr*)&peer,&len);
          if(sock < 0)
          {
            Log::LOG(WARNING,"accept get accept is fail");
            continue;
          }
        
          pthread_t p;
          int* arg = new int(sock);
          pthread_create(&p,nullptr,Entrance::HandlerRequest,(void*)arg);
      }

    }

    ~HttpServer()
    {
    }
  private:
    bool stap;
    int _port;
};
